package com.bank.action;

import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.bank.pojo.BankCards;
import com.bank.svc.BankCardSvc;
import com.bank.tools.BankCardNumGenerator;
import com.bank.tools.EncyptTools;
import com.bank.tools.JSONOutput;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller("bankCardAction")
public class BankCardAction extends ActionSupport{
	@Resource(name="bankCardSvc")
	private BankCardSvc bcs;
	private String udoc;
	private String ucash;
	private String pay_pwd;
	
	public String addNewCard() throws Exception{
		String cardId=BankCardNumGenerator.generateBankCardNum();
		BankCards card=new BankCards(cardId, EncyptTools.MessageEncypt(pay_pwd), Float.parseFloat(ucash),0,1,new Timestamp(new Date().getTime()));
		boolean flag=bcs.addNewCard(card, udoc);
		String json="";
		if(flag){
			cardId="\""+cardId.replaceAll("([\\d]{4})", "$1 ")+"\"";
			json="{\"status\":0,\"info\":\"操作成功完成！\",\"num\":"+cardId+",\"cash\":"+Float.parseFloat(ucash)+"}";
		}else{
			json="{\"status\":1,\"info\":\"用户不存在！\"}";
		}
		JSONOutput.OutputJSON(ServletActionContext.getResponse(), json);
		return "success";
	}
	
		
	public void setBcs(BankCardSvc bcs) {
		this.bcs = bcs;
	}
	public void setUdoc(String udoc) {
		this.udoc = udoc;
	}
	public void setUcash(String ucash) {
		this.ucash = ucash;
	}
	public void setPay_pwd(String pay_pwd) {
		this.pay_pwd = pay_pwd;
	}
}
